<template>
    <el-dialog v-model="visible" title="展会授权" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
            v-loading="dataForm.loading" label-width="120px">
            <el-row>
                <el-col :span="9">
                    <el-form-item label="展会项目" style="width: 320px">
                        <el-select placeholder="请选择" v-model="dataForm.projectId" clearable @change="changeProject">
                            <el-option v-for="item in exhibitionproject" :key="item.id" :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="9">
                    <el-checkbox @change="handleCheckAllChange" v-model="allStufs">全选</el-checkbox>
                </el-col>
            </el-row>
            <el-form-item style="margin-left: -70px;" prop="tskIdArr">
                <el-checkbox-group v-model="dataForm.tskIdArr" multiple>
                    <el-checkbox v-for="role in exhibitionlist" :key="role.id" :label="role.id" :value="role.id">
                        <span v-if="role.exhSName.length > 8" :title="role.exhSName">{{
                            role.exhSName.substring(0, 8) }}...</span>
                        <span v-else>{{ role.exhSName }}</span>
                    </el-checkbox>
                </el-checkbox-group>
            </el-form-item>
        </el-form>
        <template v-slot:footer>
            <div style="text-align: left;margin-left: 45px;">
                <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
                <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import baseService from "@/service/baseService";
import { isEmail, isMobile } from "@/utils/utils";
import { IObject } from "@/types/interface";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const exhibitionlist = ref<any[]>([]);
const dataFormRef = ref();
const dataForm = reactive({
    loading: false,
    id: null as any,
    roleIdList: [] as any[],
    tskIdArr: [] as any[],
    projectId: null as any,
    oldExhIdArr: [] as any[],
});

const allStufs = ref(false); // 全选

watch([
    () => dataForm.tskIdArr,
], ([newStufsList], [oldStufsList]) => {
    allStufs.value = newStufsList.length === exhibitionlist.value.length;

});

const init = (userId?: number) => {
    visible.value = true;
    dataForm.id = userId;
    if (dataFormRef.value) {
        dataFormRef.value.resetFields();
    }
    getexhibition('');
    getoperatorList();
    queryExh();
};

//必填项
const rules = ref({
    tskIdArr: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});


//全选
const handleCheckAllChange = (val: boolean) => {
    // 选中
    if (val) {
        for (let i = 0; i < exhibitionlist.value.length; i++) {
            dataForm.tskIdArr.push(exhibitionlist.value[i].id);
        }
    } else {
        dataForm.tskIdArr = [];
    }
}

const changeProject = (val: any) => {
    getexhibition(val);
}


// 获取展会列表
const getexhibition = (id: any) => {
    return baseService.post("/crm/crmexhibition/list", { exhProject: id }).then((res) => {
        exhibitionlist.value = res.data;
    });
};


//获取展会项目列表
const exhibitionproject = ref<any[]>([]);
const getoperatorList = (page = 1, limit = 10) => {
    return baseService.get(`/crm/crmexhiproject/page?page=${page}&limit=${limit}`).then((res) => {
        console.log(res.data.list)
        exhibitionproject.value = res.data.list;
    });
};

//查询用户已有的展会
const queryExh = () => {
    return baseService.post("/sys/user/queryExh", { id: dataForm.id }).then((res) => {
        dataForm.oldExhIdArr = [];
        let data = res.data;
        for (let i = 0; i < data.length; i++) {
            dataForm.tskIdArr.push(data[i].id);
            dataForm.oldExhIdArr.push(data[i].id);
        }
    });
};

// 表单提交
const dataFormSubmitHandle = () => {
    dataFormRef.value.validate((valid: boolean) => {
        if (!valid) {
            return false;
        }
        dataForm.loading = true;
        dataForm.tskIdArr = dataForm.tskIdArr.map((item: number) => Number(item));
        (baseService.post)("/sys/user/setExh", { ids: dataForm.id, exhIdArr: dataForm.tskIdArr, oldExhIdArr: dataForm.oldExhIdArr }).then((res) => {
            ElMessage.success({
                message: t("prompt.success"),
                duration: 500,
                onClose: () => {
                    visible.value = false;
                    dataForm.loading = false;
                    emit("refreshDataList");
                }
            });
        });
    });
};

defineExpose({
    init
});
</script>
<style lang="scss" scoped>
.el-checkbox {
    width: 120px;
}
</style>
